package com.pureut.production.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.production.domain.RollLengthChange;
import com.pureut.production.domain.dto.CutOrdersDocumentDetailDto;
import com.pureut.production.domain.dto.CutOrdersDocumentDto;
import com.pureut.production.domain.dto.MasterVolumeFeignDto;
import com.pureut.production.domain.dto.RollLengthChangeDto;
import com.pureut.production.mapper.CutOrdersDocumentDetailMapper;
import com.pureut.production.mapper.CutOrdersDocumentMapper;
import com.pureut.production.mapper.RollLengthChangeMapper;
import com.pureut.production.mapper.SpunlaceOutputDetailMapper;
import com.pureut.production.service.IRollLengthChangeService;
import com.pureut.production.domain.vo.RollLengthChangeVo;
import com.pureut.system.api.StorageFegin;
import com.pureut.system.api.vo.WmsStockInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: YSL
 * @Date: 2023-02-09 11:25:57
 * @Desc: 母卷变更
 */
@Service("rollLengthChangeService")
public class RollLengthChangeServiceImpl extends ServiceImpl<RollLengthChangeMapper, RollLengthChange> implements IRollLengthChangeService {

    /**
     * 母卷变更mapper
     */
    @Resource
    RollLengthChangeMapper rollLengthChangeMapper;

    /**
     * 仓库feign
     */
    @Resource
    StorageFegin storageFegin;

    /**
     * 水刺产出明细
     */
    @Resource
    private SpunlaceOutputDetailMapper spunlaceOutputDetailMapper;

    /**
     * 分切制令单
     */
    @Resource
    private CutOrdersDocumentMapper cutOrdersDocumentMapper;

    /**
     * 分期制令单明细
     */
    @Resource
    private CutOrdersDocumentDetailMapper cutOrdersDocumentDetailMapper;

    /**
     * 查询卷长变更列表
     * @param rollLengthChangeVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<RollLengthChangeDto> selectRollLengthList(RollLengthChangeVo rollLengthChangeVo) {
        return rollLengthChangeMapper.selectRollLengthList(rollLengthChangeVo);
    }

    /**
     * 新增卷长变更
     * @param rollLengthChangeVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(RollLengthChangeVo rollLengthChangeVo) {

        AjaxResult ajaxResult = storageFegin.selectMasterVolumeList(rollLengthChangeVo.getMasterVolumeNumber());
        MasterVolumeFeignDto data = JSON.parseObject(JSON.toJSONString(ajaxResult.get("data")), MasterVolumeFeignDto.class);

        // 判断母卷是否分切
        CutOrdersDocumentDetailDto cutOrdersDocumentInfo = cutOrdersDocumentDetailMapper.getCutOrdersDocumentInfo(data.getWsiMaterialNumber());
        if (!cutOrdersDocumentInfo.getSlittingStatus().equals("1")){
            throw new GlobalException("此接长母卷已分切,不可变更");
        }

        RollLengthChange rollLengthChange = new RollLengthChange();
        BeanUtils.copyProperties(rollLengthChangeVo, rollLengthChange);
        // 母卷号码
        rollLengthChange.setMasterVolumeNumber(rollLengthChangeVo.getMasterVolumeNumber());
        // 产品编码
        rollLengthChange.setMaterialCode(data.getWsiMaterialCode());
        // 产品名称
        rollLengthChange.setMaterialName(data.getWsiMaterialName());
        // 产品规格
        rollLengthChange.setMaterialSpec(data.getWsiMaterialSpec());
        // TODO 分切执行单
        rollLengthChange.setConnectionId(cutOrdersDocumentInfo.getDocumentNum());
        // 原卷长
        rollLengthChange.setOriginalRollLength(data.getWsiLength());
        // 变更后卷长
        rollLengthChange.setNewRollLength(rollLengthChangeVo.getNewRollLength());
        // 部门id
        rollLengthChange.setDeptId(SecurityUtils.getDeptId());
        // 变更人
        rollLengthChange.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        // 变更时间
        rollLengthChange.setCreateTime(new Date());

        boolean save = save(rollLengthChange);
        if (save){
            // 通过母卷号码修改库存卷长
            WmsStockInfoVo wmsStockInfoVo = new WmsStockInfoVo();
            wmsStockInfoVo.setWsiMaterialNumber(rollLengthChange.getMasterVolumeNumber());
            wmsStockInfoVo.setWsiLength(rollLengthChangeVo.getNewRollLength());
            storageFegin.updateStockInfoById(wmsStockInfoVo);

            // 修改水刺产出明细卷长
            spunlaceOutputDetailMapper.editRollLengthChange(rollLengthChange.getMasterVolumeNumber(), rollLengthChangeVo.getNewRollLength());
            // 修改分期制令单明细卷长
            cutOrdersDocumentDetailMapper.editCutOrdersRollLengthChange(rollLengthChange.getMasterVolumeNumber(), rollLengthChangeVo.getNewRollLength());
        }

        return save;
    }
}
